<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="终端编号" prop="terminalNumber">
        <el-input
          v-model="queryParams.terminalNumber"
          placeholder="请输入终端编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="序号" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入序号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户序号" prop="userid">
        <el-input
          v-model="queryParams.userid"
          placeholder="请输入用户序号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录间隔" prop="record">
        <el-input
          v-model="queryParams.record"
          placeholder="请输入记录间隔"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采集间隔" prop="collection">
        <el-input
          v-model="queryParams.collection"
          placeholder="请输入采集间隔"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传间隔" prop="upload">
        <el-input
          v-model="queryParams.upload"
          placeholder="请输入上传间隔"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下限值" prop="lowerLimit">
        <el-input
          v-model="queryParams.lowerLimit"
          placeholder="请输入下限值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传值" prop="upperLimit">
        <el-input
          v-model="queryParams.upperLimit"
          placeholder="请输入上传值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="波动值" prop="wave">
        <el-input
          v-model="queryParams.wave"
          placeholder="请输入波动值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下限值2" prop="lowerLimit2">
        <el-input
          v-model="queryParams.lowerLimit2"
          placeholder="请输入下限值2"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传值2" prop="upperLimit2">
        <el-input
          v-model="queryParams.upperLimit2"
          placeholder="请输入上传值2"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="波动值2" prop="wave2">
        <el-input
          v-model="queryParams.wave2"
          placeholder="请输入波动值2"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IP" prop="ip">
        <el-input
          v-model="queryParams.ip"
          placeholder="请输入IP"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="端口" prop="port">
        <el-input
          v-model="queryParams.port"
          placeholder="请输入端口"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="清零" prop="clear">
        <el-input
          v-model="queryParams.clear"
          placeholder="请输入清零"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字符串" prop="characterString">
        <el-input
          v-model="queryParams.characterString"
          placeholder="请输入字符串"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态 0正常 1.等待 2.失败" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态 0正常 1.等待 2.失败"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="错误日志" prop="journal">
        <el-input
          v-model="queryParams.journal"
          placeholder="请输入错误日志"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回复报文" prop="message">
        <el-input
          v-model="queryParams.message"
          placeholder="请输入回复报文"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="错误信息" prop="errorMessage">
        <el-input
          v-model="queryParams.errorMessage"
          placeholder="请输入错误信息"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者" prop="created">
        <el-input
          v-model="queryParams.created"
          placeholder="请输入创建者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改者" prop="modify">
        <el-input
          v-model="queryParams.modify"
          placeholder="请输入修改者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改时间" prop="modifyTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.modifyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="删除状态" prop="deleted">
        <el-input
          v-model="queryParams.deleted"
          placeholder="请输入删除状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mk:configuration:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mk:configuration:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mk:configuration:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['mk:configuration:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configurationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="终端编号" align="center" prop="terminalNumber" />
      <el-table-column label="序号" align="center" prop="serialNumber" />
      <el-table-column label="用户序号" align="center" prop="userid" />
      <el-table-column label="记录间隔" align="center" prop="record" />
      <el-table-column label="采集间隔" align="center" prop="collection" />
      <el-table-column label="上传间隔" align="center" prop="upload" />
      <el-table-column label="下限值" align="center" prop="lowerLimit" />
      <el-table-column label="上传值" align="center" prop="upperLimit" />
      <el-table-column label="波动值" align="center" prop="wave" />
      <el-table-column label="下限值2" align="center" prop="lowerLimit2" />
      <el-table-column label="上传值2" align="center" prop="upperLimit2" />
      <el-table-column label="波动值2" align="center" prop="wave2" />
      <el-table-column label="IP" align="center" prop="ip" />
      <el-table-column label="端口" align="center" prop="port" />
      <el-table-column label="清零" align="center" prop="clear" />
      <el-table-column label="字符串" align="center" prop="characterString" />
      <el-table-column label="状态 0正常 1.等待 2.失败" align="center" prop="state" />
      <el-table-column label="错误日志" align="center" prop="journal" />
      <el-table-column label="回复报文" align="center" prop="message" />
      <el-table-column label="错误信息" align="center" prop="errorMessage" />
      <el-table-column label="创建者" align="center" prop="created" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改者" align="center" prop="modify" />
      <el-table-column label="修改时间" align="center" prop="modifyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="删除状态" align="center" prop="deleted" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mk:configuration:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mk:configuration:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改配置下行对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="终端编号" prop="terminalNumber">
          <el-input v-model="form.terminalNumber" placeholder="请输入终端编号" />
        </el-form-item>
        <el-form-item label="序号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="用户序号" prop="userid">
          <el-input v-model="form.userid" placeholder="请输入用户序号" />
        </el-form-item>
        <el-form-item label="记录间隔" prop="record">
          <el-input v-model="form.record" placeholder="请输入记录间隔" />
        </el-form-item>
        <el-form-item label="采集间隔" prop="collection">
          <el-input v-model="form.collection" placeholder="请输入采集间隔" />
        </el-form-item>
        <el-form-item label="上传间隔" prop="upload">
          <el-input v-model="form.upload" placeholder="请输入上传间隔" />
        </el-form-item>
        <el-form-item label="下限值" prop="lowerLimit">
          <el-input v-model="form.lowerLimit" placeholder="请输入下限值" />
        </el-form-item>
        <el-form-item label="上传值" prop="upperLimit">
          <el-input v-model="form.upperLimit" placeholder="请输入上传值" />
        </el-form-item>
        <el-form-item label="波动值" prop="wave">
          <el-input v-model="form.wave" placeholder="请输入波动值" />
        </el-form-item>
        <el-form-item label="下限值2" prop="lowerLimit2">
          <el-input v-model="form.lowerLimit2" placeholder="请输入下限值2" />
        </el-form-item>
        <el-form-item label="上传值2" prop="upperLimit2">
          <el-input v-model="form.upperLimit2" placeholder="请输入上传值2" />
        </el-form-item>
        <el-form-item label="波动值2" prop="wave2">
          <el-input v-model="form.wave2" placeholder="请输入波动值2" />
        </el-form-item>
        <el-form-item label="IP" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入IP" />
        </el-form-item>
        <el-form-item label="端口" prop="port">
          <el-input v-model="form.port" placeholder="请输入端口" />
        </el-form-item>
        <el-form-item label="清零" prop="clear">
          <el-input v-model="form.clear" placeholder="请输入清零" />
        </el-form-item>
        <el-form-item label="字符串" prop="characterString">
          <el-input v-model="form.characterString" placeholder="请输入字符串" />
        </el-form-item>
        <el-form-item label="状态 0正常 1.等待 2.失败" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态 0正常 1.等待 2.失败" />
        </el-form-item>
        <el-form-item label="错误日志" prop="journal">
          <el-input v-model="form.journal" placeholder="请输入错误日志" />
        </el-form-item>
        <el-form-item label="回复报文" prop="message">
          <el-input v-model="form.message" placeholder="请输入回复报文" />
        </el-form-item>
        <el-form-item label="错误信息" prop="errorMessage">
          <el-input v-model="form.errorMessage" placeholder="请输入错误信息" />
        </el-form-item>
        <el-form-item label="创建者" prop="created">
          <el-input v-model="form.created" placeholder="请输入创建者" />
        </el-form-item>
        <el-form-item label="修改者" prop="modify">
          <el-input v-model="form.modify" placeholder="请输入修改者" />
        </el-form-item>
        <el-form-item label="修改时间" prop="modifyTime">
          <el-date-picker clearable size="small"
            v-model="form.modifyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除状态" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入删除状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfiguration, getConfiguration, delConfiguration, addConfiguration, updateConfiguration, exportConfiguration } from "@/api/mk/configuration";

export default {
  name: "Configuration",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 配置下行表格数据
      configurationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        terminalNumber: null,
        serialNumber: null,
        userid: null,
        record: null,
        collection: null,
        upload: null,
        lowerLimit: null,
        upperLimit: null,
        wave: null,
        lowerLimit2: null,
        upperLimit2: null,
        wave2: null,
        ip: null,
        port: null,
        clear: null,
        characterString: null,
        state: null,
        journal: null,
        message: null,
        errorMessage: null,
        created: null,
        createTime: null,
        modify: null,
        modifyTime: null,
        deleted: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询配置下行列表 */
    getList() {
      this.loading = true;
      listConfiguration(this.queryParams).then(response => {
        this.configurationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        terminalNumber: null,
        serialNumber: null,
        userid: null,
        record: null,
        collection: null,
        upload: null,
        lowerLimit: null,
        upperLimit: null,
        wave: null,
        lowerLimit2: null,
        upperLimit2: null,
        wave2: null,
        ip: null,
        port: null,
        clear: null,
        characterString: null,
        state: null,
        journal: null,
        message: null,
        errorMessage: null,
        created: null,
        createTime: null,
        modify: null,
        modifyTime: null,
        deleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加配置下行";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConfiguration(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改配置下行";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConfiguration(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfiguration(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除配置下行编号为"' + ids + '"的数据项？').then(function() {
        return delConfiguration(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有配置下行数据项？').then(() => {
        this.exportLoading = true;
        return exportConfiguration(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
